package com.heima.article.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.common.dto.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.service.IApArticleService;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 文章信息表，存储已发布的文章 前端控制器
 * </p>
 *
 * @author lj
 * @since 2022-06-29
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/article")
@Api(tags = "文章信息表，存储已发布的文章接口")
@CrossOrigin
public class ApArticleController{

    @Autowired
    private IApArticleService apArticleService;

    /**
     * 保存文章
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation(value = "保存文章", notes = "author:liJie")  // value指名称  notes 备注
    @ApiImplicitParam(name = "dto", value = "保存文章dto", required = true, dataType = "ArticleDto")
    public ResponseResult<Long> saveArticle(@RequestBody ArticleDto dto){
        log.info("[文章微服务======>保存文章，接收参数：{}]",dto.toString());
        return apArticleService.saveArticle(dto);
    }

    // 根据发布时间实现不同操作功能，直接列表展示，下拉更新，上滑更新
    /**
     * 首页加载文章列表
     * @param dto
     * @return
     */
    @PostMapping("/load")
    @ApiOperation(value = "首页加载文章列表", notes = "author:liJie")  // value指名称  notes 备注
    @ApiImplicitParam(name = "dto", value = "前端数据", required = true, dataType = "ArticleHomeDto")
    public ResponseResult load(@RequestBody ArticleHomeDto dto){
        return apArticleService.load(dto,0);//发布时间大于maxTime
    }

    /**
     * 首页加载文章-下拉更新
     * @param dto
     * @return
     */
    @PostMapping("/loadnew")
    @ApiOperation(value = "首页加载文章-下拉更新", notes = "author:liJie")  // value指名称  notes 备注
    @ApiImplicitParam(name = "dto", value = "前端数据", required = true, dataType = "ArticleHomeDto")
    public ResponseResult loadnew(@RequestBody ArticleHomeDto dto){
        return apArticleService.load(dto,0); //发布时间大于maxTime
    }

    /**
     * 首页加载文章-上滑更多
     * @param dto
     * @return
     */
    @PostMapping("/loadmore")
    @ApiOperation(value = "首页加载文章-上滑更多", notes = "author:liJie")  // value指名称  notes 备注
    @ApiImplicitParam(name = "dto", value = "前端数据", required = true, dataType = "ArticleHomeDto")
    public ResponseResult loadmore(@RequestBody ArticleHomeDto dto){
        return apArticleService.load(dto,1); //发布时间小于minTime
    }

    /**
     * 文章详情
     * @param dto
     * @return
     */
    @PostMapping("/load_article_info")
    @ApiOperation(value = "文章详情", notes = "author:liJie")  // value指名称  notes 备注
    @ApiImplicitParam(name = "dto", value = "文章详情接收数据", required = true, dataType = "ArticleInfoDto")
    public ResponseResult loadArticleInfo(@RequestBody ArticleInfoDto dto){
        return apArticleService.loadArticleInfo(dto);
    }
}
